﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;

namespace ExaminationSys
{
    public partial class Search : System.Web.UI.Page
    {
        private static readonly StudentService Proxy = ObjectFactory<StudentService>.Create();
        private static readonly NewsTypeService ProxyNewsTypeService = ObjectFactory<NewsTypeService>.Create();
        private static readonly NewsService ProxyNewsService = ObjectFactory<NewsService>.Create();
        public List<News> NewsCollection = new List<News>();
        public StringBuilder htmlStr = new StringBuilder();
        public StringBuilder htmlLoginBox = new StringBuilder();//
       
        protected void Page_Load(object sender, EventArgs e)
        {
            var keyWords = Request.QueryString["q"];
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(keyWords))
                {
                    keyWords = keyWords.SqlSafe();
                    NewsCollection = ProxyNewsService.GetNewsByKeyWords(keyWords);
                }
                var loginInfo = Session["StudentInfo"] as Student;
                if (loginInfo != null)
                {
                    htmlLoginBox = UIHelpers.BuildLoginBoxHtml(loginInfo);
                }
                else
                {
                    HttpCookie cookie = Request.Cookies["SJEDU_UID"];
                    if (cookie != null)
                    {
                        var uid = cookie["UID"];
                        var pwd = cookie["PWD"];

                        var student = Proxy.Login(uid, pwd);
                        if (student != null)
                        {
                            htmlLoginBox = UIHelpers.BuildLoginBoxHtml(student);
                            Session["StudentInfo"] = student;
                        }
                    }
                }
            }
        }
    }
}